'''
CSS位置偏移反爬案例分析与爬取实战
'''
# from selenium import webdriver
# from selenium.webdriver.common.by import By
# from selenium.webdriver.support import expected_conditions as EC
# from selenium.webdriver.support.wait import WebDriverWait
# from selenium.webdriver import ChromeOptions
# from selenium.webdriver.chrome.service import Service
# from pyquery import PyQuery as pq
# import re
#
# def parse_name(name_html):
#     has_whole = name_html('.whole')
#     if has_whole:
#         return name_html.text()
#     else:
#         chars = name_html('.char')
#         items = []
#         for char in chars.items():
#             items.append({
#                 'text': char.text().strip(),
#                 'left': int(re.search('(\d+)px', char.attr('style')).group(1))
#             })
#         items = sorted(items, key = lambda x:x['left'], reverse = False)
#         return ''.join([item.get('text') for item in items])
#
# driver_path = 'D:\install\Python\Scripts\chromedriver.exe'
# options = ChromeOptions()
# options.add_experimental_option('excludeSwitches', ['enable-automation'])
# options.add_experimental_option('useAutomationExtension', False)
# options.add_argument('--headless')
# browser = webdriver.Chrome(service=Service(driver_path),options=options)
# browser.get('https://antispider3.scrape.center/')
# WebDriverWait(browser, 10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.item')))
# html = browser.page_source
# doc = pq(html)
# names = doc('.item .name')
# for name_html in names.items():
#     name = parse_name(name_html)
#     print(name)
# browser.close()




'''
字体反爬案例分析与爬取实战
针对源代码中不包含信息，存在::before（伪节点）的案例
'''
from selenium import webdriver
from pyquery import PyQuery as pq
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import re
import requests
# 获取::before的映射关系
url = 'https://antispider4.scrape.center/css/app.654ba59e.css'
response = requests.get(url)
pattern = re.compile('.icon-(.*?):before\{content:"(.*?)"\}')
results = re.findall(pattern,response.text)
icon_map = {item[0]:item[1] for item in results}

def parse_score(item):
    elements = item('.icon')
    icon_values = []
    for element in elements.items():
        class_name = (element.attr('class'))
        icon_key = re.search('icon-(\d+)', class_name).group(1)
        icon_value = icon_map.get(icon_key)
        icon_values.append(icon_value)
    return ''.join(icon_values)


browser = webdriver.Chrome()
browser.get('https://antispider4.scrape.center/')
WebDriverWait(browser,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '.item')))
html = browser.page_source
doc = pq(html)
items = doc('.item')
for item in items.items():
    name = item('.name').text()
    categories = [o.text() for o in item('.categories button').items()]
    score = parse_score(item)
    print(f'name:{name} categories:{categories} score:{score}')
browser.close()